package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.VisitorService;
/**
 * Servlet implementation class ViewEventServlets
 */
@WebServlet("/ViewEventServlets")
public class ViewEventServlets extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewEventServlets() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		EventService es = new EventService();
		Object iD = request.getSession().getAttribute("iDVisitor");
		int idevent = Integer.parseInt(request.getParameter("idevent"));
	    int id= (Integer) iD;
	    				
		try{
			ArrayList<Integer> myidevents = es.viewMyEvent(id);
			int size = myidevents.size();
			boolean signed=false;
			int iter=0;
			while (iter<size && signed==false){
				if(idevent==myidevents.get(iter)){
					signed=true;
				}else{
					signed=false;
				}
				iter++;
			}
			String myString = null; 
	
			myString = String.valueOf(signed);
			request.setAttribute("signed",myString);
			
			Event evento = es.viewEvent(idevent);
			request.setAttribute("evento", evento);
			RequestDispatcher dispatcher = request
	                .getRequestDispatcher("/viewevent.jsp"); 
			if (dispatcher != null){  
			      dispatcher.forward(request, response);  
			}
										
		}catch(Exception ex){
				ex.getMessage();
				
		}
		 
		
		 
		 
	} 
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	    
	}

}
